<template>
    <router-view v-if="!$router.currentRoute.value.meta.layout" />
    <el-container v-else-if="$router.currentRoute.value.meta.layout === 'SinglePage'">
        <el-aside
            :width="sidebarWidth + 'px'"
            class="sidebar smooth-animation"
        >
            <logo
                v-if="systemSettings.sidebarLogo"
                class="logo-block smooth-animation"
                :style="[{width: sidebarWidth + 'px'}]"
            />
            <router-view name="Sidebar" />
        </el-aside>
        <el-container>
            <el-main style="padding: 0">
                <el-scrollbar
                    height="96vh"
                    class="flex-body smooth-animation"
                    :style="{'margin-left': sidebarWidth + 'px', width: 'calc(100vw - ' + sidebarWidth + 'px)'}"
                >
                    <router-view
                        name="NavBar"
                        class="smooth-animation"
                    />
                    <router-view
                        name="TagsView"
                        class="smooth-animation"
                    />

                    <router-view
                        v-if="mainPageAlive"
                        v-slot="{ Component }"
                        name="Content"
                        style="height: 100%; margin-left: 30px; margin-right: 60px;"
                    >
                        <transition
                            v-if="mainPageAlive"
                            enter-active-class="animate__animated animate__fadeIn animate__faster"
                        >
                            <component :is="Component" />
                        </transition>
                    </router-view>
                </el-scrollbar>
            </el-main>
        </el-container>
        <div
            class="global-settings"
            @click="drawer = true"
        >
            <i class="el-icon-setting" />
        </div>
        <el-drawer
            v-model="drawer"
            size="20%"
            :with-header="false"
            destroy-on-close
        >
            <trends-form
                class="setting-form"
                :form-data="systemSettings"
                :form-controls="formControls"
                :form-settings-data="formSettingData"
            />
        </el-drawer>
    </el-container>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Logo from './components/sidebar/Logo'
export default {
    name: "App",
    components: {Logo},
    data() {
        return {
            drawer: false,
            formControls: [{"widthPx":40,"prev":false,"type":"Switch","disabled":false,"label":"固定Header","activeValue":true,"inactiveValue":false,"name":"fixedHeader","width":"46%","id":1632157392657,"selected":true,"inBoxMove":false},{"widthPx":40,"prev":false,"type":"Switch","disabled":false,"label":"侧边栏Logo","activeValue":true,"inactiveValue":false,"name":"sidebarLogo","width":"46%","id":1632157420319,"selected":false,"inBoxMove":false},{"widthPx":36,"prev":false,"type":"Switch","disabled":false,"label":"开启TagView","activeValue":true,"inactiveValue":false,"name":"openTagView","width":"46%","id":1632157334059,"selected":false,"inBoxMove":false}],
            formSettingData: {"inline":false,"labelPosition":"left","labelWidth":100,"hideRequiredAsterisk":false,"showMessage":true,"inlineMessage":false,"size":"medium","disabled":false}
        }
    },
    computed: {
        ...mapState({
            // 左侧菜单是否关闭
            collapse: state => state.collapse,
            // 左侧菜单宽度
            sidebarWidth: state => state.sidebarWidth,
            // 系统设置
            mainPageAlive: state => state.mainPageAlive,
            // 系统设置
            systemSettings: state => state.systemSettings,
            // 左侧菜单默认宽度
            defaultSidebarWidth: state => state.defaultSidebarWidth
        }),
        bodyTop() {
            if (this.systemSettings.fixedTagView) {
                return this.systemSettings.openTagView ? '106px' : '64px';
            }
            return this.systemSettings.openTagView ? '60px' : '';
        }
    },
    watch: {
        'systemSettings.fixedHeader'() {
            this.systemSettings.fixedTagView = this.systemSettings.fixedHeader;
        },
        collapse() {
            if (this.collapse) {
                this.changeSidebarWidth(64 + this.sidebarWidth * 0.04)
            } else {
                this.changeSidebarWidth(this.defaultSidebarWidth)
            }
        }
    },
    mounted() {
        window.addEventListener('keydown', this.myKeyEvent);

        if (document.exitFullscreen) {
            document.addEventListener('fullscreenchange', this.changeFullscreenVar)
        } else if (document.mozCancelFullScreen) {
            document.addEventListener('mozfullscreenchange', this.changeFullscreenVar)
        } else if (document.msExitFullscreen) {
            document.addEventListener('MSFullscreenChange', this.changeFullscreenVar)
        } else if (document.webkitCancelFullScreen) {
            document.addEventListener('webkitfullscreenchange', this.changeFullscreenVar)
        }
    },
    beforeUnmount() {
        window.removeEventListener('keydown', this.myKeyEvent);
        if (document.exitFullscreen) {
            document.removeEventListener('fullscreenchange', this.changeFullscreenVar)
        } else if (document.mozCancelFullScreen) {
            document.removeEventListener('mozfullscreenchange', this.changeFullscreenVar)
        } else if (document.msExitFullscreen) {
            document.removeEventListener('MSFullscreenChange', this.changeFullscreenVar)
        } else if (document.webkitCancelFullScreen) {
            document.removeEventListener('webkitfullscreenchange', this.changeFullscreenVar)
        }
    },
    methods: {
        myKeyEvent(e) {
            let key = e.keyCode;
            if (key === 122) {
                // 阻止默认的键盘事件
                event.returnValue = false;
                this.$store.commit('triggerFullScreen')
            }
        },
        changeFullscreenVar() {
            this.$store.commit('changeFullScreen')
        },
        fullScreenChange() {
            // 判断一下变量是不是全屏，是全屏则代表 F11、ESC 退出了全屏，需要变量更正过来
            if (this.$store.state.btnFullScreen && this.$store.state.fullscreen) {
                this.$store.commit('changeFullScreen')
            }
        },
        change() {
            this.$store.dispatch('nameInfo');
        },
        ...mapMutations({
            changeSidebarWidth: 'changeSidebarWidth'
        })
    }
}
</script>

<style>
    body {
        margin: 0;
        padding: 0;
    }
    .smooth-animation {
        transition: 0.4s linear;
        transition-property: margin-left, width, left;
    }
    .sidebar {
        top: 0;
        left: 0;
        z-index: 5;
        height: 100vh;
        overflow-x: hidden;
        position: absolute;
        padding-bottom: 10%;
        scrollbar-width: none;
        background-color: #191a23;
        box-shadow: 3px 0 8px -1px rgba(0,0,0,0.69);
        -moz-box-shadow: 3px 0 8px -1px rgba(0,0,0,0.69);
        -webkit-box-shadow: 3px 0 8px -1px rgba(0,0,0,0.69);
    }
    .sidebar::-webkit-scrollbar {
        display: none;
    }
    .logo-block {
        /*position: fixed;*/
        z-index: 10;
        background-color: #191a23;
    }
    .flex-body {
        position: fixed;
    }
    ::v-deep(.el-scrollbar__bar) {
        z-index: 5;
    }
    .global-settings {
        top: 43%;
        left: 100%;
        z-index: 5;
        width: 40px;
        height: 40px;
        font-size: 30px;
        position: fixed;
        line-height: 40px;
        padding: 8px 0 10px 12px;
        background-color: #7979a8;
        transform: translate(-100%, -50%);
    }

    .global-settings i::before {
        z-index: 1;
        color: white;
    }

    .global-settings:hover {
        box-shadow: -2px 0 4px rgba(120, 121, 167, 0.5), 0 0 6px rgba(120, 121, 167, 0.3);
    }
    .setting-form {
        margin: 30px 0 0 30px;
    }
</style>
